// pages/preview/preview.js
const app = getApp()
const baseUrls = 'http://soundlab.jschunk.com:8085';
var server = require('../../utils/server');
const recorderManager = wx.getRecorderManager()
const innerAudioContext = wx.createInnerAudioContext()
const backgroundAudioManager = wx.getBackgroundAudioManager()
innerAudioContext.autoplay = true
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tu: app.globalData.tupian,
    list:{},
    moren:0,
    // yinyue: app.globalData.tupian + 'yy.png',
    // yinyue1: app.globalData.tupian + 'yy1.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    that = this
    console.log(e)
    var li = decodeURIComponent(e.list)
    var list = JSON.parse(li)
    that.setData({
      list : list,
      moren:e.moren
    })
  },

  bofang1: function (e) {
    console.log('开始播放')
    backgroundAudioManager.title = that.data.list.title
    backgroundAudioManager.src = that.data.list.soundPath,
    backgroundAudioManager.play()
  },
  fb:function(e){
    wx.showLoading({
      title: '正在提交',
    })
    if (that.data.moren === '1') {
      console.log('jjjjjjjj')
      that.shangchuan(that.data.list.picPath)
    } else {   
      console.log('111111')
      that.fabu()   
    }
  },
  shangchuan: function (sscc) {
    console.log('111', sscc)
    server.uploadFile(sscc, function (e) {
      console.log('22', e)
      var obj = JSON.parse(e.data); // --&gt; parse error 
      console.log('上传成功tu', e, '++++++++++++', obj)
      if (obj.code == 0) {
        console.log('obj', obj)
        that.data.list.picPath = obj.data.url
        that.fabu()

      } else {
        wx.showToast({
          title: obj.message,
          icon: 'none',
          duration: 1000,
        });
      }
    })
  },
  fabu:function(e){
    console.log('上传参数',that.data.list)
    server.requestForm(false, 'post', 'card',
      that.data.list,
      function (e) {
        console.log(e)
        if (e.code === 1500) {
          wx.hideLoading()
          wx.showToast({
            title: e.errMsg,
            icon: 'none',
            duration: 1000,
          });
          return;
        }else if(e.code === 1501){
          wx.hideLoading()
          wx.showToast({
            title: e.message,
            icon: 'none',
            duration: 1000,
          });
          // console.log(e.message);
          return;
        }else if ( e.code!== 0){
          wx.hideLoading()
          wx.showToast({
            title: '发布失败！',
            icon: 'none',
            duration: 1000,
          });
          console.log(e.message);
          return;
        }

        let id = e._id || e.data._id;
        wx.reLaunch({
          url: '../carddetails/carddetails?show=' + true + '&id=' + id,
        })
      })
  }
 
})